package com.hxy.travel.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.travel.dao.TravelGroupDao;
import com.hxy.travel.dao.TravelItemDao;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.pojo.TravelGroup;
import com.hxy.travel.pojo.TravelItem;
import com.hxy.travel.service.TravelGroupService;
import com.hxy.travel.service.TravelItemService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = TravelGroupService.class)
public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupDao travelGroupDao;


    @Override
    public void addTravelGroupAndIds(TravelGroup travelGroup, Integer[] travelItemIds) {

        travelGroupDao.addTravelGroup(travelGroup);

        System.out.println(travelGroup.toString());

        if (travelItemIds.length > 0){

            for (Integer travelItemId : travelItemIds) {

                Map<String,Integer> map = new HashMap<>();

                map.put("travelItemId",travelItemId);

                map.put("travelGroupId",travelGroup.getId());

                travelGroupDao.addTravelGroupAndItem(map);
            }
        }
    }



    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage() == null ? 1 : queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize() == null ? 10 : queryPageBean.getPageSize();

        PageHelper.startPage(currentPage,pageSize);


        List<TravelGroup> TravelGroups = travelGroupDao.findPage(queryPageBean.getQueryString());

        PageInfo<TravelGroup> pageInfo = new PageInfo<>(TravelGroups);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public TravelGroup selectById(Integer id) {
        return travelGroupDao.selectById(id);
    }

    @Override
    public void editTravelGroup(TravelGroup travelGroup, Integer[] travelItemIds) {
        travelGroupDao.editTravelGroup(travelGroup);

        travelGroupDao.deleteTravelGroupAndItemById(travelGroup.getId());

        if (travelItemIds.length > 0){

            for (Integer travelItemId : travelItemIds) {

                Map<String,Integer> map = new HashMap<>();

                map.put("travelItemId",travelItemId);

                map.put("travelGroupId",travelGroup.getId());

                travelGroupDao.addTravelGroupAndItem(map);

            }
        }
    }

    @Override
    public void deleteTravelItemById(Integer id) {
        int count = travelGroupDao.selectTravelItemByItemId(id);
        if (count > 0){
            throw new RuntimeException("数据关联不可删除！");
        }
        travelGroupDao.deleteTravelItemById(id);
    }

    @Override
    public List<TravelGroup> findAll() {
        return travelGroupDao.findAll();
    }

    @Override
    public List<TravelItem> selectTravelGroupList() {
        return travelGroupDao.selectTravelGroupList();
    }

    @Override
    public List<Integer> selectTravelGroupBySetmealId(Integer id) {
        return travelGroupDao.selectTravelGroupBySetmealId(id);
    }


//
//    @Override
//    public void deleteTravelItemById(Integer id) {
//        travelItemDao.deleteTravelItemById(id);
//    }
//
//    @Override
//    public TravelItem selectById(Integer id) {
//        return travelItemDao.selectById(id);
//    }
//
//    @Override
//    public void editTravelItem(TravelItem travelItem) {
//        travelItemDao.editTravelItem(travelItem);
//    }
//
//    @Override
//    public List<TravelItem> selectTravelItemList() {
//        return travelItemDao.selectAllTravelItemList();
//    }
}
